package accountManagement.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletLogout
 */
@WebServlet("/ServletLogout")
public class ServletLogout extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletLogout() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

    private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	Object user = request.getSession().getAttribute("user");
    	if(user == null) {
    		request.setAttribute("error", "Non disponi dei privilegi necessari per accedere alla pagina richiesta");
    		request.getRequestDispatcher("/errorPage.jsp").forward(request, response);
    	} else {
    		request.getSession().removeAttribute("user");
    		request.setAttribute("resultTitle", "Logout");
    		request.setAttribute("resultContent", "Logout riuscito");
    		request.getRequestDispatcher("/result.jsp").forward(request, response);
    	}
    }
}
